--numero de inverciones en un arreglo en que sus componentes son diferentes
evalDC :: Eq a => (a -> Bool) -> (a -> b) -> (a -> [a]) -> ([b] -> b) -> a -> b
evalDC isBase base split join xss | isBase xss = base xss
                 | otherwise = join (map (evalDC isBase base split join) (split xss))

inverciones :: [Int] -> ([Int],Int)
inverciones xs = evalDC (isbase) (base) (split) (join) xs
----------------------------------------------------------------------------------------------------------------
split :: [Int] -> [[Int]]
split xs = [(take (div (length xs) 2) xs),(drop (div (length xs) 2) xs)]
----------------------------------------------------------------------------------------------------------------
-- toma una lista de elementos y  pasa a base si esta es menor a uno o vacia
isbase :: Eq a => [a] -> Bool
isbase (x:xs) =  length (x:xs) <= 1
----------------------------------------------------------------------------------------------------------------
-- genera una tupla ,  con la primer componente xs y la segunda su numero de inversiones
base :: [Int] -> ([Int],Int)
base xs = (xs, 0)
----------------------------------------------------------------------------------------------------------------
join :: [([Int],Int)] -> ([Int],Int)
join [(xs,n),(ys,m)] = merge (n+m) xs ys
           
merge :: Int -> [Int] -> [Int] -> ([Int],Int)
merge a xs [] = (xs, a)
merge a [] ys = (ys,a)
merge a (x:xs) (y:ys) | y<x = (y:fst t,snd t)
                      | x<y = (x:fst r,snd r)
            where
            t = merge (a+length(x:xs)) (x:xs) (ys)   
            r = merge (a) (xs) (y:ys)


